package com.aote.controller;
//验证码操作

import com.aote.constant.MessageConstant;
import com.aote.constant.RedisMessageConstant;
import com.aote.entity.Result;
import com.aote.utils.TencentSMSUtils;
import com.aote.utils.ValidateCodeUtils;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;
//    用户在线体检预约发送验证码
    @RequestMapping("/send40rder")
    public Result send4Order(String telephone){
//        随机生成数字验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
//        给用户发送验证码
        try {
            TencentSMSUtils.sendShortMessage(TencentSMSUtils.VALIDATE_CODE,telephone,code.toString());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
//        将验证码保存到redis(5分钟)
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

//    用户登录发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        try {
            TencentSMSUtils.sendShortMessage(TencentSMSUtils.LOGIN_CODE,telephone,code.toString());
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
//        将验证码保存到redis
        jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,300,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
